package org.abettor.octopus.ui.proxy;

import org.abettor.octopus.ui.MainDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

/**
 * 主窗口的操作行为代理类
 * @author abettor
 *
 */
public class MainDialogProxy
{
	private MainDialog mainDlg;
	private Display display;
	private int threadNumber;
	private String connector;
	private String connectionString;
	private String user;
	private String pass;
	private Boolean reversed;

	/**
	 * 默认构造函数
	 * @param mainDlg 要代理的主窗口类的对象引用
	 */
	public MainDialogProxy(MainDialog mainDlg)
	{
		this.mainDlg = mainDlg;
		display = mainDlg.getDisplay();
	}

	/**
	 * 获取主窗口对象句柄
	 */
	public Shell getMainShell()
	{
		return mainDlg.getShell();
	}

	/**
	 * 设定为已经自然完成状态
	 */
	public void setAccomplished()
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				mainDlg.setStopped();
			}
		};

		if(! display.isDisposed())
		{
			display.syncExec(thrd);
		}
	}

	/**
	 * 设定破解进度
	 * @param percent 当前进度百分点，不小于0，不大于100
	 */
	public void setProgress(final int percent)
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				ProgressBar progress = mainDlg.getProgress();
				progress.setSelection(percent);
			}
		};

		if(! display.isDisposed())
		{
			display.asyncExec(thrd);
		}
	}

	/**
	 * 获取要启动的破解线程数
	 */
	public int getThreadNumber()
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				threadNumber = mainDlg.getThread();
			}
		};

		if(! display.isDisposed())
		{
			display.syncExec(thrd);
			return threadNumber;
		}

		return 0;
	}

	/**
	 * 获取用于连接数据库的标识名称
	 */
	public String getConnector()
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				connector = mainDlg.getConnector();
			}
		};

		if(! display.isDisposed())
		{
			display.syncExec(thrd);
			return connector;
		}

		return null;
	}

	/**
	 * 获取数据库连接字符串
	 */
	public String getConnectionString()
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				connectionString = mainDlg.getUrl();
			}
		};

		if(! display.isDisposed())
		{
			display.syncExec(thrd);
			return connectionString;
		}

		return null;
	}

	/**
	 * 获取用户字典路径
	 */
	public String getUserDic()
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				user = mainDlg.getUser();
			}
		};

		if(! display.isDisposed())
		{
			display.syncExec(thrd);
			return user;
		}

		return null;
	}

	/**
	 * 获取密码字典路径
	 */
	public String getPassDic()
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				pass = mainDlg.getPass();
			}
		};

		if(! display.isDisposed())
		{
			display.syncExec(thrd);
			return pass;
		}

		return null;
	}

	/**
	 * 检查是否应该使用逆向模式
	 */
	public boolean isReversedMode()
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				reversed = mainDlg.isReversedMode();
			}
		};

		if(! display.isDisposed())
		{
			display.syncExec(thrd);
			return reversed.booleanValue();
		}

		return false;
	}

	/**
	 * 向结果列表里添加记录
	 * @param user 用户名
	 * @param pass 密码
	 */
	public void addToResultTable(final String user, final String pass)
	{
		Runnable thrd = new Runnable()
		{
			public void run()
			{
				String[] content = new String[] {user, pass};
				TableItem item = new TableItem(mainDlg.getResultTable(), SWT.NONE);
				item.setText(content);
			}
		};

		if(! display.isDisposed())
		{
			display.asyncExec(thrd);
		}
	}
}
